package com.basketball.gym.listener;


import com.basketball.gym.pojo.state.RedisHeader;
import com.basketball.gym.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import java.nio.charset.StandardCharsets;


public class KeyExpiredListener extends KeyExpirationEventMessageListener {
    private OrderService orderService;
    private static final Logger LOGGER = LoggerFactory.getLogger(KeyExpiredListener.class);


    public KeyExpiredListener(RedisMessageListenerContainer listenerContainer, OrderService redisService) {
        super(listenerContainer);
        this.orderService = redisService;
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        System.out.println(message);
        String channel = new String(message.getChannel(), StandardCharsets.UTF_8);
        //过期的key
        String key = new String(message.getBody(), StandardCharsets.UTF_8);
        System.out.println(key);
        String[] keySplit = key.split("__");
        if (keySplit.length != 2) {
            return;
        }
        if (keySplit[0].equals("order")) {
            orderService.cancelOrder(Integer.valueOf(keySplit[1]));
        }
    }
}